# -*- coding: utf-8 -*-
import subprocess

from flask import json
import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops

from facility.reg_fn import get_number


# bingfa shezhi wei 1
class BingchuanSpider(RedisSpider):
    name = 'bingchuan'
    redis_key = 'bingchuan:start_urls'
    v8host = 'http://localhost:5000/'

    con = redis.Redis(host='localhost', port=6379, db=0)
    con.delete(redis_key)
    con.lpush(redis_key, v8host)

    def parse(self, response):
        yield Request(url='http://localhost:5000/bingchuan/next', dont_filter=True,
                      callback=self.parse_end)

    def parse_item(self, response):
        if 'bingchuan' in response.url:
            q = Ops(response)
            for i in range(q.get_num('//*[@id="conRptList"]/li')):
                name = q.get_str('//*[@id="conRptList"]/li[{}]/a/span'.format(i + 1))
                amount = q.get_str('//*[@id="conRptList"]/li[{}]/table/tbody/tr/td[3]/p[1]/span'.format(i + 1))
                rate = q.get_str('//*[@id="conRptList"]/li[{}]/table/tbody/tr/td[1]/p[1]/span/font[1]'.format(i + 1))
                cycle = q.get_str('//*[@id="conRptList"]/li[{}]/table/tbody/tr/td[2]/p[1]/span[1]'.format(i + 1))
                data = name + ' ' + amount + ' ' + rate + ' ' + cycle
                subprocess.call("echo  {} >>冰川.txt".format(data), shell=True)

    def parse_end(self, response):
        q = Ops(response)
        item = q.get_num('//*[@id="conRptList"]/li')
        for i in range(item):
            yield Request(url='http://localhost:5000/bingchuan/{}'.format(i + 1), dont_filter=True,
                          callback=self.parse_detail)

        yield Request(url='http://localhost:5000/bingchuan/next', dont_filter=True,
                      callback=self.parse_end)

    def parse_detail(self, response):
        q = Ops(response)
        name = q.get_str('//*[@id="title"]')
        amount = q.get_str('//*[@id="amount"]')
        rate = q.get_str('//*[@id="rate"]')
        start = q.get_str('//*[@id="publishDate"]').split(" ")[0].split("\t")[0]
        cycle = q.get_str('//*[@id="period"]') + q.get_str('//*[@id="periodType"]')
        way = q.get_str('//*[@id="repayTypeExp"]')
        data = str(name) + " " + str(amount) + " " + str(rate) + " " + str(start) + " " + str(cycle) + " " + str(way)
        subprocess.call("echo  {} >>冰川.txt".format(data), shell=True)
